import {AsyncStorage} from 'react-native'
import {kAuthorization, kContentType} from "../constant";
import {AuthModel, UserModel} from "./model";
import Request from "../utils/Request";

const {Toast} = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_user";
export default {
    namespace: 'user',
    state: {
        loading: false,
        follow: {
            followers: 0,
            following: 0,
            isFollow: false,
            isMutual: false
        },
        colleague: [],
        info: null,
        hobbyList: null,
        skillList: null,
        hotMembers: []
    },
    reducers: {
        save(state, {payload}) {
            return {...state, ...payload}
        }
    },
    effects: {
        // 保存到本地
        * saveToStorage({payload}, {call}) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload))
        },

        // 本地恢复
        * restoreFromStorage({payload}, {call, put, select}) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: {...JSON.parse(data)}
            });
        },


        //关注或许取消关注
        * follow({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            payload['params'] = {...payload};
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            payload[kContentType] = 'application/json';
            const follow = yield call(Request.post, Request.getApi().follow, payload);
            if (follow) {
                yield put({type: 'isFollow', payload: payload['params']});
            }
        },

        //是否已经关注某人,并返回此人关注者，关注的人数
        * isFollow({payload}, {call, put, select}) {
            let follow = {followers: 0, following: 0, isFollow: false, isMutual: false};
            yield put({type: 'save', payload: {follow}});
            const auth: AuthModel = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            follow = yield call(Request.get, Request.getApi().isFollow, payload);
            if (follow) {
                yield put({type: 'save', payload: {follow}});
            }
        },

        //获取我的同事
        * fetchColleague({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            let p = {};
            p[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const colleague = yield call(Request.get, Request.getApi().colleague + `/${payload.uid}/${payload.cid}`, p);
            if (colleague) {
                if (Array.isArray(colleague)) {
                    yield put({type: 'save', payload: {colleague}})
                }
            }
        },


        //获取用户信息
        * fetchUserInfo({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            yield put({type: 'save', payload: {info: null, loading: true}});
            const result: userItem = yield call(Request.get, Request.getApi().userInfoWithId + `/${payload.id}`, payload);
            yield put({type: 'save', payload: {loading: false}});
            if (result && typeof result !== 'boolean') {
                yield put({
                    type: 'save',
                    payload: {
                        info: result
                    }
                })
            }
        },

        * fetchTags({payload}, {call, put, select}) {
            const auth: AuthModel = yield select(state => state.auth);
            const user: UserModel = yield select(state => state.user);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const type = payload.type;
            const result = yield call(Request.get, Request.getApi().tags + `/${payload.type}`, payload);
            if (result) {
                if (type === 1) {
                    user.skillList = result
                } else if (type === 2) {
                    user.hobbyList = result
                }

                yield put({
                    type: 'save',
                    payload: {...user}
                })
            }

        },

        * fetchHotMembers({payload}, {call, put, select}) {
            if (!payload) payload = {};
            const auth: AuthModel = yield select(state => state.auth);
            payload[kAuthorization] = auth.token_type + ' ' + auth.access_token;
            const hotMembers: userItem[] = yield call(Request.get, Request.getApi().hotMembers, payload);
            if (hotMembers) {
                yield put({
                    type: 'save',
                    payload: {hotMembers}
                })
            }

        },

    },
    subscriptions: {
        setup({dispatch}) {

        }
    },
};
